package cn.itcast.order.web;

import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;

   @Autowired
   private RestTemplate restTemplate;

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {

        Order order = orderService.queryOrderById(orderId);

        //拼接获取user数据请求路径
        String url="http://userService:8081/user/"+order.getUserId(); //使用eureka注册服务名称远程调用
        //使用restTemplate根据指定url获取响应的user json格式数据，并将其序列化为user对象
        User use = restTemplate.getForObject(url, User.class);
        //将user对象数据封装到order对象中
        order.setUser(use);

        // 根据id查询订单并返回
        return order;
    }
}
